package com.oauth.service.members.dao;

import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import com.oauth.service.jdosupport.PMF;
import com.oauth.service.members.beans.Member;
import com.oauth.service.members.constants.Gender;

public class MemberDAOImpl implements MemberDAO {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.spradeep.profile.dao.impl.MemberDAO#createMember(java.lang.String,
	 * java.lang.String, java.lang.String, java.util.Date,
	 * com.spradeep.profile.constants.Gender, java.lang.String)
	 */
	public Member createMember(String firstName, String lastName,
			String password, Date birthDate, Gender gender, String email) {
		Member member = new Member(null, firstName, lastName, password, null,
				new Date(), birthDate, new Date(), gender, email, new Date());
		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();
		try {
			member = persistenceManager.makePersistent(member);
		} finally {
			persistenceManager.close();
		}
		return member;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.spradeep.profile.dao.impl.MemberDAO#updateMember(com.spradeep.profile
	 * .beans.Member)
	 */
	public boolean updateMember(Member member) {
		// Set the last update date to current date.
		member.setLastUpdateDate(new Date());
		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();
		try {
			member = persistenceManager.makePersistent(member);
		} finally {
			persistenceManager.close();
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.spradeep.profile.dao.impl.MemberDAO#getMember(java.lang.Long)
	 */
	public Member getMember(Long memberId) {
		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();

		return getMember(memberId, persistenceManager);

	}

	/**
	 * This method is created to handle transactionally getting the member item.
	 * The transaction is maintained by the persistence manager. So, we are
	 * having it in one of the arguments.
	 * 
	 * @param memberId
	 * @param persistenceManager
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private Member getMember(Long memberId,
			PersistenceManager persistenceManager) {
		Query query = persistenceManager.newQuery(Member.class);
		query.setFilter("memberId == memberIdParam");
		query.declareParameters("Long memberIdParam");
		List<Member> results;
		results = (List<Member>) query.execute(memberId);
		if (results == null || results.isEmpty()) {
			return null;
		}
		return results.get(0);
	}

	/**
	 * This method is created to handle transactionally getting the member item.
	 * The transaction is maintained by the persistence manager. So, we are
	 * having it in one of the arguments.
	 * 
	 * @param memberId
	 * @param persistenceManager
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private Member getMember(String pEmailId,
			PersistenceManager persistenceManager) {
		Query query = persistenceManager.newQuery(Member.class);
		query.setFilter("email == emailParam");
		query.declareParameters("String emailParam");
		List<Member> results;
		results = (List<Member>) query.execute(pEmailId);
		if (results == null || results.isEmpty()) {
			return null;
		}
		return results.get(0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.spradeep.profile.dao.impl.MemberDAO#deleteMember(java.lang.Long)
	 */
	public boolean deleteMember(Long memberId) {
		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();
		Transaction tx = persistenceManager.currentTransaction();

		try {
			tx.begin(); // Begin the transaction
			Member member = getMember(memberId, persistenceManager); // check
																		// its
																		// existence
			if (member == null) {
				// No member exists with the given id.
				return false;
			}
			persistenceManager.deletePersistent(member); // Delete if exists.
			tx.commit();
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			persistenceManager.close();
		}

		return true;
	}

	
	public Member getMember(String pEmailId) {
		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();
		Member member;
		try {
			member = getMember(pEmailId,persistenceManager);
		} finally {
			persistenceManager.close();
		}
		return member;
	}

	
	public Member getMember(String pEmailId, boolean pAutoCreate) {
		Member member = getMember(pEmailId);
		if (member == null && pAutoCreate) {
			member = createMember(null, null, null, null, null, pEmailId);
		}
		return member;
	}

}
